//复制文本方法 传对象 和文本
import {
	ElMessage
} from 'element-plus';
import VueCookies from 'vue-cookies';

var userId = VueCookies.get('userId');
var headImage = VueCookies.get('headImage');
export {
	userId,
	headImage
}

//判断是否是移动端 
export const isMobile = () => {
	return !!navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)
}

export function copyText(e, text) {
	e.value = text;
	e.select();
	if (document.execCommand("Copy")) {
		ElMessage({
			type: 'success',
			message: "复制成功",
			duration: 500,
		})
		return true;
	} else {
		ElMessage({
			type: 'error',
			message: "复制失败",
			duration: 500,
		})
	}
	return false;
}

//手机号隐藏
export function phoneHide(phone) {
	var str = "" + phone;
	return str.substring(0, 3) + "****" + str.substring(7, 12);
}

//存储大小格式
export function sizeFormat(size) {
	if (size == null || size == "") {
		return "" + 0;
	}
	if (size < 1024) {
		return "" + size + "B";
	}
	if (size >= 1024 && size < 1024 * 1024) {
		return "" + (size / 1024).toFixed(2) + "KB"
	}
	if (size >= 1024 * 1024 && size < 1024 * 1024 * 1024) {
		return "" + (size / 1024 / 1024).toFixed(2) + "MB"
	}
	if (size >= 1024 * 1024 * 1024 && size < 1024 * 1024 * 1024 * 1024) {
		return "" + (size / 1024 / 1024 / 1024).toFixed(2) + "GB"
	} else {
		return "" + (size / 1024 / 1024 / 1024 / 1024).toFixed(2) + "TB"
	}
}
//点赞超过10000变w
export function likeFormat(data) {
	if (data < 0) {
		return 0;
	}
	if (data < 10000) {
		return data;
	} else {
		let newData = data / 10000;
		return newData.toFixed(1) + "W";
	}
}
//日期格式输出 date:时间 ，输出类型
export function dateFormat(date, type) {
	var from = new Date(date)
	var y = from.getFullYear()
	var mon = from.getMonth() + 1
	var d = from.getDate()
	var hour = from.getHours()
	var min = from.getMinutes()
	var sec = from.getSeconds()

	if (type == 1) {
		return "" + y + " 年 " + mon + " 月 " + d + " 日   " + hour + ":" + min + ":" + sec
	}
	if (type == 2) {
		return "" + y + "-" + mon + "-" + d + " " + hour + ":" + min + ":" + sec
	}
	if (type == 3) {
		return "" + y + "-" + mon + "-" + d
	}
	if (type == 4) {
		var nowTime = new Date().getTime();
		var fromTime = from.getTime();
		var diff = nowTime - fromTime;
		if (diff < 60 * 1000) {
			return "刚刚";
		} else if (diff < 60 * 60 * 1000) {
			return "" + Math.floor(diff / (60 * 1000)) + "分钟前";
		} else if (diff < 24 * 60 * 60 * 1000) {
			return "" + Math.floor(diff / (60 * 60 * 1000)) + "小时前";
		} else {
			return "" + y + "-" + mon + "-" + d
		}
	}
	//聊天里用的
	if (type == 5) {
		var nowTime1 = new Date().getTime();
		var fromTime1 = from.getTime();
		var diff1 = nowTime1 - fromTime1;
		//在30分钟内都不提示时间
		if (diff1 < 60 * 1000 * 30) {
			return "";
		} else if (diff1 < 60 * 60 * 1000) {
			return "" + Math.floor(diff1 / (60 * 1000)) + "分钟前";
		} else {
			return "" + y + "-" + mon + "-" + d + " " + hour + ":" + min + ":" + sec
		}
	}
}

export function dateSub(dateOld, dateNew, type) {
	var oldDate = new Date(dateOld).getTime();
	var newDate = new Date(dateNew).getTime();
	if (type == 1) {
		return newDate - oldDate;
	}
	//大于30分钟true
	if (type == 2) {
		var cha = newDate - oldDate;
		if (cha > 1000 * 60 * 30) {
			return true;
		} else {
			return false;
		}
	}
}

// 将文件转换成功base64
export function getBase64(file) {
	return new Promise((resolve, reject) => {
		let reader = new FileReader()
		let fileResult = ''
		reader.readAsDataURL(file)
		reader.onload = function () {
			fileResult = reader.result
		}
		reader.onerror = function (error) {
			reject(error)
		}
		reader.onloadend = function () {
			resolve(fileResult)
		}
	})
}